package org.darkforest.common.services.impl;

import org.darkforest.common.services.SettingService;
import org.darkforest.common.services.SimpleRedisService;
import org.darkforest.common.util.SystemConstant;
import org.darkforest.dao.mapper.SettingMapper;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.model.Setting;
import org.darkforest.vo.SiteSettingInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by valentina on 15/4/24.
 */
@Service("settingServiceImpl")
public class SettingServiceImpl implements SettingService{

    @Autowired
    private SimpleRedisService redisService;

    @Autowired
    private SettingMapper settingMapper;

    @Override
    public Integer saveSetting(Setting setting) {
        return settingMapper.insert(setting);
    }

    @Override
    public Integer updateSetting(Setting setting) {
        //同时更新redis
        redisService.setValue(setting.getName(),setting.getValue());
        return settingMapper.updateByPrimaryKeyWithBLOBs(setting);
    }

    @Override
    public String getSettingInfo(SettingNameEnum name) {
        //优先从redis中获取，redis中没有数据的话从数据库查出并同步到redis中
        String val = redisService.getByKey(name.getName());
        if(val==null){
            Setting setting = settingMapper.selectByPrimaryKey(name.getName());
            val = setting.getValue();
            //放入到redis中
            redisService.setValue(name.getName(),val);
        }
        return val;
    }

    @Override
    public List<Setting> findAllSetting() {
        return settingMapper.findAllSetting();
    }

    @Override
    public void initSiteSetting(SiteSettingInfo siteSettingInfo) {
        siteSettingInfo.setSiteLogo(getSettingInfo(SettingNameEnum.SITE_LOGO));
        siteSettingInfo.setSiteName(getSettingInfo(SettingNameEnum.SITE_NAME));
        siteSettingInfo.setCompanyName(getSettingInfo(SettingNameEnum.SITE_COMPANY));
        siteSettingInfo.setIcpName(getSettingInfo(SettingNameEnum.ICP_NUMBER));
        siteSettingInfo.setServicePhone(getSettingInfo(SettingNameEnum.SITE_PHONE));
        siteSettingInfo.setServiceEmail(getSettingInfo(SettingNameEnum.SITE_EMAIL));
        siteSettingInfo.setPcwebSite(getSettingInfo(SettingNameEnum.PCWEBSITE));
        siteSettingInfo.setMerchantSite(getSettingInfo(SettingNameEnum.MERCHANTSITE));

        //邮箱设置
        SystemConstant.HOST=getSettingInfo(SettingNameEnum.EMAIL_HOST);// "smtp.mxhichina.com";
        SystemConstant.USERNAME=getSettingInfo(SettingNameEnum.EMAIL_ID);// "service@huayunsky.com";
        SystemConstant.PASSWORD=getSettingInfo(SettingNameEnum.EMAIL_PASS);// "1q2w3e4r+.";
        SystemConstant.SENDER=getSettingInfo(SettingNameEnum.EMAIL_ADDR);// "service@huayunsky.com";
        SystemConstant.PORT=getSettingInfo(SettingNameEnum.EMAIL_PORT);// "25";
        siteSettingInfo.setHost(getSettingInfo(SettingNameEnum.EMAIL_HOST));
        siteSettingInfo.setUserName(getSettingInfo(SettingNameEnum.EMAIL_ID));
        siteSettingInfo.setPassword(getSettingInfo(SettingNameEnum.EMAIL_PASS));
        siteSettingInfo.setSender(getSettingInfo(SettingNameEnum.EMAIL_ADDR));
        siteSettingInfo.setPort(getSettingInfo(SettingNameEnum.EMAIL_PORT));


    }
}
